from collections import namedtuple

from osc02.resource import MonitoredFilterStore
from osc02.resource.Resources import ResourceUtil


class ConstructionSite:
    def __init__(self, env, name, worker_teams=None, grout_gears=None, crane_num=1, storage_yard=None,
                 activities=None) -> object:
        self.name = name
        self.env = env
        self.storage_yard = storage_yard
        self.grout_gears = grout_gears
        self.worker_teams = worker_teams
        self.activities = activities
        self.crane = ResourceUtil.get_stations(self.env, capacity=crane_num)
        # logging.basicConfig(level=logging.DEBUG)
        # logging.basicConfig(level=logging.DEBUG)

    def __repr__(self):
        return self.name


class Factory:
    def __init__(self, env, components, moving_mold_platen=None):
        self.env = env
        # self.worker_availability = {'A01': 9, 'A02': 10, 'A03': 3}
        # self.machine_availability = {'A01': 9, 'A02': 10, 'A03': 3}
        # initial stackyard
        # self.stackyard = self.config_store()
        self.stackyard = None
        self.steam_machine = self.config_steam_machine()
        self.components = components
        self.production_line = None
        # self.moving_mold_platen = moving_mold_platen
        # logging.basicConfig(level=logging.DEBUG)
        # logging.basicConfig(level=logging.DEBUG)

    def config_steam_machine(self):
        Slot = namedtuple('Slot', 'type')
        s1 = Slot(1)

        # steam_machine = simpy.FilterStore(self.env, capacity=20)
        steam_machine = MonitoredFilterStore(self.env, capacity=20)

        # create steam machine
        steam_machine.items = []
        for i in range(20):
            steam_machine.items.append(s1)

        # logging.debug('INIT:items={0}, len={1}'.format(steam_machine.items, len(steam_machine.items)))
        return steam_machine


class MovingMoldPlaten:
    @staticmethod
    def get_platens(env, type=1, amount=10):
        Slot = namedtuple('Slot', 'type')
        s1 = Slot(1)

        # steam_machine = simpy.FilterStore(self.env, capacity=20)
        moving_mold_platen = MonitoredFilterStore(env, capacity=amount)

        # create steam machine
        moving_mold_platen.items = []
        for i in range(amount):
            moving_mold_platen.items.append(s1)

        # logging.debug('INIT:items={0}, len={1}'.format(moving_mold_platen.items, len(moving_mold_platen.items)))
        return moving_mold_platen


# the time can be estimated with map-based service
class Transportation:
    def __init__(self, env, factory, construction_site, transport_truck_num=1, transport_time_mean=3,
                 transport_time_std=1):
        self.env = env
        self.factory = factory
        self.construction_site = construction_site
        self.transport_truck_num = transport_truck_num
        self.transport_time_mean = transport_time_mean
        self.transport_time_std = transport_time_std
        self.fleet = self.get_fleet(type=1)

    def get_fleet(self, type=1):
        Slot = namedtuple('Slot', 'type')
        s1 = Slot(1)

        # steam_machine = simpy.FilterStore(self.env, capacity=20)
        fleet = MonitoredFilterStore(self.env, capacity=self.transport_truck_num)

        # create steam machine
        fleet.items = []
        for i in range(self.transport_truck_num):
            fleet.items.append(s1)

        # logging.debug('INIT:items={0}, len={1}'.format(fleet.items, len(fleet.items)))
        return fleet
